package vista.treeDiagrama;

import java.awt.Component;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

@SuppressWarnings("serial")
public class TreeRenderParaDiagrama extends DefaultTreeCellRenderer {

	public TreeRenderParaDiagrama()
	{
		super();
	}
	
	
	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
	{		
		super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
		
		if (value != null && value instanceof DefaultMutableTreeNode)
		{
			DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;  
	        
	        if (node.getUserObject() instanceof NodoTreeDiagrama)
	        {
	        	NodoTreeDiagrama elemento = (NodoTreeDiagrama) node.getUserObject();
	        	
	        	this.cargarImagen(expanded, leaf, elemento);
  			}
        }     
		
		return this;
	}
	
	private void cargarImagen(boolean expanded, boolean leaf, NodoTreeDiagrama elemento)
	{
		String path = null;
		
		if (leaf)
		{
			path = elemento.getPathArchivoImagenHoja();
		}
		else if (expanded)
		{
			path = elemento.getPathArchivoImagenAbierto();
		}
		else
		{
			path = elemento.getPathArchivoImagenCerrado();
		}
		
				        	 
	   URL imgURL = getClass().getResource(path);
	        	 
  		if (imgURL != null) 
     		setIcon( new ImageIcon(imgURL));
  		else 
  			System.err.println(this.getClass()+" No se encontró el archivo: " + elemento.getPathArchivoImagenHoja());
		
	}

}
